package com.lwt.hospital.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwt.hospital.dao.DemoDao;
import com.lwt.hospital.model.Admin;
import com.lwt.hospital.model.Doctor;
import com.lwt.hospital.model.User;
import com.lwt.hospital.service.Demo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 丁兆顺
 * @date 2020/5/18 1:17
 * @desciption
 */
@Service
public class DemoImpl implements Demo {

    @Resource
    private DemoDao demoDao;

    @Override
    public List<User> getAllUserInfo() {
        List<User> users = demoDao.selectAllUser();
        System.out.println(users);
        return null;
    }

    @Override
    public boolean login(User user, HttpSession session) {
        Map<String, String> cardMap = null;
        Map<String, String> phoneMap = null;
        if (user.getIdentity() == 1) {
            List<User> users = demoDao.selectAllUser();
            cardMap = users.stream().collect(Collectors.toMap(User::getCard, User::getPassword));
            phoneMap = users.stream().collect(Collectors.toMap(User::getPhone, User::getPassword));
            if (cardMap.containsKey(user.getAccount())) {
                String pass = cardMap.get(user.getAccount());
                if (user.getPassword().equals(pass)) {
                    User userInfo = demoDao.selectUserByCard(user.getAccount());
                    userInfo.setIdentity(user.getIdentity());
                    session.setAttribute("userInfo", userInfo);
                    return true;
                } else {
                    return false;
                }
            } else if (phoneMap != null && phoneMap.containsKey(user.getAccount()) && user.getIdentity() == 1) {
                String pass = phoneMap.get(user.getAccount());
                if (user.getPassword().equals(pass)) {
                    User userInfo = demoDao.selectUserByPhone(user.getAccount());
                    userInfo.setIdentity(user.getIdentity());
                    session.setAttribute("userInfo", userInfo);
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (user.getIdentity() == 2) {
            List<Doctor> docs = demoDao.selectAllDoc();
            cardMap = docs.stream().collect(Collectors.toMap(Doctor::getNumber, Doctor::getPassword));
            if (cardMap.containsKey(user.getAccount())) {
                String pass = cardMap.get(user.getAccount());
                if (user.getPassword().equals(pass)) {
                    Doctor userInfo = demoDao.selectDocByNumber(user.getAccount());
                    userInfo.setIdentity(user.getIdentity());
                    session.setAttribute("userInfo", userInfo);
                    return true;
                } else {
                    return false;
                }
            }else{
                return false;
            }
        } else {
            List<Admin> admins = demoDao.selectAllAdmin();
            cardMap = admins.stream().collect(Collectors.toMap(Admin::getCard, Admin::getPassword));

            if (cardMap.containsKey(user.getAccount())) {
                String pass = cardMap.get(user.getAccount());
                if (user.getPassword().equals(pass)) {
                    Admin userInfo = demoDao.selectAdminByCard(user.getAccount());
                    userInfo.setIdentity(user.getIdentity());
                    session.setAttribute("userInfo", userInfo);
                    return true;
                } else {
                    return false;
                }
            }else{
                return false;
            }
        }

    }

}
